package com.wll.ec.fast.detail;

import android.graphics.Bitmap;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

import com.blankj.utilcode.util.ScreenUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.wll.ec.core.ui.recycler.ItemType;
import com.wll.ec.core.ui.recycler.MultipleFields;
import com.wll.ec.core.ui.recycler.MultipleItemEntity;
import com.wll.ec.core.ui.recycler.MultipleRecyclerAdapter;
import com.wll.ec.core.ui.recycler.MultipleViewHolder;
import com.wll.ec.fast.R;

import java.util.List;

/**
 * created by detachment on 2020/3/27
 */
public class RecyclerImageAdapter extends MultipleRecyclerAdapter {

    private static final RequestOptions OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .fitCenter()
            .dontAnimate();

    protected RecyclerImageAdapter(List<MultipleItemEntity> data) {
        super(data);
        addItemType(ItemType.SINGLE_BIG_IMAGE, R.layout.item_image);
    }

    @Override
    protected void convert(MultipleViewHolder holder, MultipleItemEntity entity) {
        super.convert(holder, entity);
        final int type = holder.getItemViewType();
        switch (type) {
            case ItemType.SINGLE_BIG_IMAGE:
                final AppCompatImageView imageView = holder.getView(R.id.image_rv_item);
                final String url = entity.getField(MultipleFields.IMAGE_URL);

                Glide.with(mContext)
                        .asBitmap()
                        .load(url)
                        .apply(OPTIONS)
//                        .into(imageView);
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                int imageWidth = resource.getWidth();
                                int imageHeight = resource.getHeight();
                                int height = ScreenUtils.getScreenWidth() * imageHeight / imageWidth;
                                ViewGroup.LayoutParams para = imageView.getLayoutParams();
                                if (para != null) {
                                    para.height = height;
                                    imageView.setLayoutParams(para);
                                }
                                imageView.setImageBitmap(resource);
//                                Glide.with(Ec.getApplicationContext())
//                                        .load(url)
//                                        .apply(OPTIONS)
//                                        .into(imageView);
                            }
                        });
                break;
            default:
                break;
        }
    }
}
